home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.04 Apr 88 / TearOffPalette Source / Initialize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-13  |  9.1 KB  |  349 lines  |  [TEXT/KAHL]

  1. /*
  2. ----------------------------------------------------------------------------------------------------
  3. T E A R O F F   P A L E T T E
  4.  
  5.     version 1.0
  6.     by Don Melton and Mike Ritter
  7.     
  8.     Copyright (C)1987, 1988 by Impulse Technologies, Inc., all rights reserved. 
  9.     
  10.     Filename:            Initialize.c
  11.     Font:                    Monaco, 9 point
  12.     Tab setting:    2
  13.     Compiler:            LightspeedC 2.15, Project type: APPL, Creator: TOPD
  14.     Segment:            Initialize
  15.     
  16.     IMPORTANT! Use ResEdit to set the code resource of this segment to unlocked. */
  17.  
  18.  
  19.  
  20. /*
  21. ----------------------------------------------------------------------------------------------------
  22. GLOBAL CONSTANT DEFINITIONS AND VARIABLE DECLARATIONS */
  23.  
  24. #include "Constants.h"
  25. #include "Variables.h"
  26.  
  27. /*
  28. ----------------------------------------------------------------------------------------------------
  29. EXTERNAL FUNCTION DECLARATIONS */
  30.  
  31. extern short GoodNewPtr();                                    /* Error.c */
  32. extern short GoodNewHandle();                                /* Error.c */
  33. extern short GoodResource();                                /* Error.c */
  34. extern pascal long RecoverMemory();                    /* Error.c */
  35. extern pascal void DrawToolMenu();                    /* Menu.c */
  36. extern pascal short FindToolItem();                    /* Menu.c */
  37. extern pascal void HiliteToolItem();                /* Menu.c */
  38. extern pascal void DrawPatternMenu();                /* Menu.c */
  39. extern pascal short FindPatternItem();            /* Menu.c */
  40. extern pascal void HilitePatternItem();            /* Menu.c */
  41. extern pascal void DrawColorMenu();                    /* Menu.c */
  42. extern pascal short FindColorItem();                /* Menu.c */
  43. extern pascal void HiliteColorItem();                /* Menu.c */
  44.  
  45. /*
  46. ----------------------------------------------------------------------------------------------------
  47. FORWARD FUNCTION DECLARATIONS */
  48.  
  49. void Initialize();
  50. void SetupEnvironment();
  51. void SetupMemory();
  52. void SetupPalettes();
  53. void SetupToolPalette();
  54. void SetupPatternPalette();
  55. void SetupColorPalette();
  56. void SetupPaletteRects();
  57. void SetupMenus();
  58.  
  59.  
  60.  
  61. /*
  62. ----------------------------------------------------------------------------------------------------
  63. INITIALIZE */
  64.  
  65. void Initialize() {
  66.  
  67.     InitGraf(&thePort);
  68.     InitFonts();
  69.     InitWindows();
  70.     InitMenus();
  71.     TEInit();
  72.     InitDialogs(nil);
  73.     InitCursor();
  74.     
  75.     FlushEvents(everyEvent, 0);
  76.     
  77.     SetupEnvironment();
  78.     SetupMemory();
  79.     SetupPalettes();
  80.     SetupMenus();
  81.  
  82.     TopWindow = TopPalette = TopDocument = nil;
  83.     BottomPalette = BRING_TO_FRONT;
  84.     
  85.     PositionCounter = 0;
  86.     
  87.     Sleep = SLEEP_DURATION;
  88.     
  89.     ClosingAll = Quitting = Finished = false;
  90. }
  91.  
  92. /*
  93. ----------------------------------------------------------------------------------------------------
  94. SETUP ENVIRONMENT */
  95.  
  96. void SetupEnvironment() {
  97.  
  98.     if(SysEnvirons(VERSION_REQUESTED, &Environment) == envBadSel) {
  99.         ExitToShell();
  100.     }
  101.     if(Environment.machineType >= envMachUnknown) {
  102.         WNEIsImplemented = NGetTrapAddress(WAIT_NEXT_EVENT_TRAP_NUMBER, ToolTrap)
  103.                 != NGetTrapAddress(UNIMPLEMENTED_TRAP_NUMBER, ToolTrap);
  104.     }
  105.     else {
  106.         WNEIsImplemented = false;
  107.     }
  108. }
  109.  
  110. /*
  111. ----------------------------------------------------------------------------------------------------
  112. SETUP MEMORY */
  113.  
  114. void SetupMemory() {
  115.  
  116.     MaxApplZone();
  117.     
  118.     MoreMasters();
  119.     MoreMasters();
  120.     
  121.     if(!GoodNewHandle(MemoryBuffer = NewHandle(MEMORY_BUFFER_SIZE))) {
  122.         ExitToShell();
  123.     }
  124.     OutOfMemory = false;
  125.     
  126.     /* Setup a simple grow zone function. */
  127.     SetGrowZone(RecoverMemory);
  128. }
  129.  
  130. /*
  131. ----------------------------------------------------------------------------------------------------
  132. SETUP PALETTES */
  133.  
  134. void SetupPalettes() {
  135.     short index;
  136.     TearOffMGlobalsPtr whichTearOff;
  137.     Rect windowRects[PALETTE_COUNT];
  138.     short attributes;
  139.     
  140.     for(index = 0; index < PALETTE_COUNT; index++) {
  141.     
  142.         /* Get a handle to the TearOffMenuGlobals structure. */
  143.         if(!GoodResource(TearOffs[index]
  144.                 = (TearOffMGlobalsHdl) GetResource(TEAR_OFF_MENU_GLOBALS_TYPE,
  145.                 TOOL_MENU_ID + index))) {
  146.             ExitToShell();
  147.         }
  148.         MoveHHi(TearOffs[index]);
  149.         HLock(TearOffs[index]);
  150.         whichTearOff = *TearOffs[index];
  151.         
  152.         switch(index) {
  153.         
  154.             case TOOL_PALETTE:
  155.                 SetupToolPalette(whichTearOff, &windowRects[index]);
  156.                 break;
  157.                 
  158.             case PATTERN_PALETTE:
  159.                 SetupPatternPalette(whichTearOff, &windowRects[index]);
  160.                 break;
  161.                 
  162.             case COLOR_PALETTE:
  163.                 SetupColorPalette(whichTearOff, &windowRects[index]);
  164.         }
  165.         if(Environment.hasColorQD) {
  166.             /* Create a color port. */
  167.             if(!GoodNewPtr(Palettes[index] = NewCWindow(nil,
  168.                     &windowRects[index],
  169.                     NULL,
  170.                     NOT_VISIBLE,
  171.                     /* Calculate the procedure ID based on the PaletteWDEF resource ID. */
  172.                     (16 * PALETTE_WDEF_ID) + noGrowDocProc,
  173.                     BRING_TO_FRONT,
  174.                     GO_AWAY_BOX,
  175.                     (long) index))) {
  176.                 ExitToShell();
  177.             }
  178.         }
  179.         else {
  180.             /* Create an old-style port. */
  181.             if(!GoodNewPtr(Palettes[index] = NewWindow(nil,
  182.                     &windowRects[index],
  183.                     NULL,
  184.                     NOT_VISIBLE,
  185.                     /* Calculate the procedure ID based on the PaletteWDEF resource ID. */
  186.                     (16 * PALETTE_WDEF_ID) + noGrowDocProc,
  187.                     BRING_TO_FRONT,
  188.                     GO_AWAY_BOX,
  189.                     (long) index))) {
  190.                 ExitToShell();
  191.             }
  192.         }
  193.         /* Finish Initializing the TearOffMenuGlobals structure. */
  194.         whichTearOff->environment = &Environment;
  195.         whichTearOff->paletteWindow = Palettes[index];
  196.         
  197.         HUnlock(TearOffs[index]);
  198.     }
  199. }
  200.  
  201. /*
  202. ----------------------------------------------------------------------------------------------------
  203. SETUP TOOL PALETTE */
  204.  
  205. void SetupToolPalette(whichTearOff, windowRect)
  206. TearOffMGlobalsPtr whichTearOff;
  207. Rect *windowRect;
  208. {
  209.     PicHandle toolPicture;
  210.     
  211.     if(!GoodResource(toolPicture = GetPicture(TOOL_PICT_ID))) {
  212.         ExitToShell();
  213.     }
  214.     HNoPurge(toolPicture);
  215.     
  216.     /* Calculate the palette's portRect based on the toolPicture. */
  217.     *windowRect = (*toolPicture)->picFrame;
  218.     OffsetRect(windowRect, -windowRect->left, -windowRect->top);
  219.     
  220.     SetupPaletteRects(&ToolRects,
  221.             TOOLS_ACROSS,
  222.             TOOLS_DOWN,
  223.             (windowRect->right + 1) / TOOLS_ACROSS,
  224.             (windowRect->bottom + 1) / TOOLS_DOWN);
  225.             
  226.     /* Initialize the TearOffMenuGlobals structure. */
  227.      whichTearOff->currentItem = DEFAULT_TOOL;
  228.     whichTearOff->drawMenuProc = DrawToolMenu;
  229.     whichTearOff->findItemProc = FindToolItem;
  230.     whichTearOff->hiliteItemProc = HiliteToolItem;
  231. }
  232.  
  233. /*
  234. ----------------------------------------------------------------------------------------------------
  235. SETUP PATTERN PALETTE */
  236.  
  237. void SetupPatternPalette(whichTearOff, windowRect)
  238. TearOffMGlobalsPtr whichTearOff;
  239. Rect *windowRect;
  240. {
  241.     /* Calculate the palette's portRect based on the number and order of patterns. */
  242.     windowRect->left = 0;
  243.     windowRect->top = 0;
  244.     windowRect->right = (PATTERNS_ACROSS * (PATTERN_ITEM_WIDTH + 1)) + 1;
  245.     windowRect->bottom = (PATTERNS_DOWN * (PATTERN_ITEM_HEIGHT + 1)) + 1;
  246.     
  247.     SetupPaletteRects(&PatternRects,
  248.             PATTERNS_ACROSS,
  249.             PATTERNS_DOWN,
  250.             PATTERN_ITEM_WIDTH + 1,
  251.             PATTERN_ITEM_HEIGHT + 1);
  252.             
  253.     /* Initialize the TearOffMenuGlobals structure. */
  254.      whichTearOff->currentItem = DEFAULT_PATTERN;
  255.     whichTearOff->drawMenuProc = DrawPatternMenu;
  256.     whichTearOff->findItemProc = FindPatternItem;
  257.     whichTearOff->hiliteItemProc = HilitePatternItem;
  258. }
  259.  
  260. /*
  261. ----------------------------------------------------------------------------------------------------
  262. SETUP COLOR PALETTE */
  263.  
  264. void SetupColorPalette(whichTearOff, windowRect)
  265. TearOffMGlobalsPtr whichTearOff;
  266. Rect *windowRect;
  267. {
  268.     /* Calculate the palette's portRect based on the number and order of colors. */
  269.     windowRect->left = 0;
  270.     windowRect->top = 0;
  271.     windowRect->right = (COLORS_ACROSS * (COLOR_ITEM_WIDTH + 1)) + 1;
  272.     windowRect->bottom = (COLORS_DOWN * (COLOR_ITEM_HEIGHT + 1)) + 1;
  273.     
  274.     SetupPaletteRects(&ColorRects,
  275.             COLORS_ACROSS,
  276.             COLORS_DOWN,
  277.             COLOR_ITEM_WIDTH + 1,
  278.             COLOR_ITEM_HEIGHT + 1);
  279.             
  280.     /* Initialize the TearOffMenuGlobals structure. */
  281.      whichTearOff->currentItem = DEFAULT_COLOR;
  282.     whichTearOff->drawMenuProc = DrawColorMenu;
  283.     whichTearOff->findItemProc = FindColorItem;
  284.     whichTearOff->hiliteItemProc = HiliteColorItem;
  285. }
  286.  
  287. /*
  288. ----------------------------------------------------------------------------------------------------
  289. SETUP PALETTE RECTS */
  290.  
  291. void SetupPaletteRects(whichRects, itemsAcross, itemsDown, itemWidth, itemHeight)
  292. Rect *whichRects;
  293. short itemsAcross;
  294. short itemsDown;
  295. short itemWidth;
  296. short itemHeight;
  297. {
  298.     short index;
  299.     short across;
  300.     short down;
  301.     
  302.     whichRects->left = 0;
  303.     whichRects->top = 0;
  304.     whichRects->right = 0;
  305.     whichRects->bottom = 0;
  306.     
  307.     index = 1;
  308.     
  309.     for(down = 0; down < itemsDown; down++) {
  310.     
  311.         for(across = 0; across < itemsAcross; across++) {
  312.             (whichRects + index)->left = across * itemWidth;
  313.             (whichRects + index)->top = down * itemHeight;
  314.             (whichRects + index)->right = (across + 1) * itemWidth;
  315.             (whichRects + index)->bottom = (down + 1) * itemHeight;
  316.             index += 1;
  317.         }
  318.     }
  319. }
  320.  
  321. /*
  322. ----------------------------------------------------------------------------------------------------
  323. SETUP MENUS */
  324.  
  325. void SetupMenus() {
  326.     short index;
  327.     
  328.     for(index = APPLE_MENU_INDEX; index < MENU_COUNT - 1; index++) {
  329.         Menus[index] = GetMenu(index + MENU_ID_OFFSET);
  330.         InsertMenu(Menus[index], 0);
  331.     }
  332.     AddResMenu(Menus[APPLE_MENU_INDEX], 'DRVR');
  333.     
  334.     ColorMenuVisible = false;
  335.     
  336.     if(Environment.hasColorQD) {
  337.         MaxDevice = GetMaxDevice(&(*GrayRgn)->rgnBBox);
  338.         
  339.         Menus[COLOR_MENU_INDEX] = GetMenu(COLOR_MENU_ID);
  340.         
  341.         if((*(*MaxDevice)->gdPMap)->pixelSize == 8) {
  342.             /* Insert the Color menu if the screen is capable of displaying 256 colors. */
  343.             InsertMenu(Menus[COLOR_MENU_INDEX], 0);
  344.             ColorMenuVisible = true;
  345.         }
  346.     }
  347.     DrawMenuBar();
  348. }
  349.